#include <string.h>
#include <stdio.h>
#include "tools.h"

char *binary_value(int value, char *string_value)
{
	strcpy(string_value, " 3322222222221111111111\n");
	strcat(string_value, " 10987654321098765432109876543210\nb");
	//strcat(string_value, convert2binary(value, bin_string_value));
 return string_value;
}

char *convert2binary(int value, char *bin_string_value)
{
	int i,j;
	j = 0;
	for(i=32-1; i >= 0; i--)
	{
		if ( value & (1<<i) )
			bin_string_value[j++] = '1';
		else
			bin_string_value[j++] = '0';
	}
	bin_string_value[j] = '\0';
 return bin_string_value;
}

char *word2ascii(int word, char *buffer)
{
	int i, cent_mille, dix_mille, mille, cent, dix, unite;
	cent_mille = word/100000;
	dix_mille = (word-cent_mille*100000)/10000;
	mille = (word-cent_mille*100000-dix_mille*10000)/1000;
	cent = (word-cent_mille*100000-dix_mille*10000-mille*1000)/100;
	dix = (word-cent_mille*100000-dix_mille*10000-mille*1000-cent*100)/10;
	unite = (word-cent_mille*100000-dix_mille*10000-mille*1000-cent*100-dix*10);
	i = 0;
	if ( word < 0 )
		buffer[i++] = '-';
	buffer[i++] = cent_mille+48;
	buffer[i++] = dix_mille+48;
	buffer[i++] = mille+48;
	buffer[i++] = cent+48;
	buffer[i++] = dix+48;
	buffer[i++] = unite+48;
	buffer[i++] = '\0';
 return buffer;
}

char *byte2ascii(char byte, char *buffer)
{
	char cent, dix, unite;
	cent = byte/100;
	dix = (byte-cent*100)/10;
	unite = (byte-cent*100-dix*10);
	buffer[0] = cent+48;
	buffer[1] = dix+48;
	buffer[2] = unite+48;
	buffer[3] = '\0';
 return buffer;
}


void fast_memaddress2hexstr(void *ptr, char *hexa_str)
{
    unsigned long addr = (unsigned long)ptr;
	unsigned long div_par_16, reste, hexaindex;
	unsigned long i, swap;
	char hexadigit;
	hexaindex = 0;
	do
	{
		div_par_16 = addr/16;
		reste = addr-(div_par_16)*16UL;
		switch ( reste )
		{
			case 0 : hexadigit = '0'; break;
			case 1 : hexadigit = '1'; break;
			case 2 : hexadigit = '2'; break;
			case 3 : hexadigit = '3'; break;
			case 4 : hexadigit = '4'; break;
			case 5 : hexadigit = '5'; break;
			case 6 : hexadigit = '6'; break;
			case 7 : hexadigit = '7'; break;
			case 8 : hexadigit = '8'; break;
			case 9 : hexadigit = '9'; break;
			case 10 : hexadigit = 'A'; break;
			case 11 : hexadigit = 'B'; break;
			case 12 : hexadigit = 'C'; break;
			case 13 : hexadigit = 'D'; break;
			case 14 : hexadigit = 'E'; break;
			case 15 : hexadigit = 'F'; break;
		}
		hexa_str[hexaindex++] = hexadigit;
		addr =  div_par_16;
	} while ( addr > 0 );
	hexa_str[hexaindex++] = 'x';
	hexa_str[hexaindex++] = '0';
hexa_str[hexaindex] = '\0';

//printf("HEXA = [%s]\n", hexa_str);
// reverse
for(i = 0; i < hexaindex/2; i++)
{
	  swap = hexa_str[i];
	  hexa_str[i] = hexa_str[(hexaindex-1)-i];
	  hexa_str[(hexaindex-1)-i] = swap;
}
printf("HEXA = [%s]\n", hexa_str);
}

// FAST -> PAS DE VERIFICATION , l'argument DOIT ETRE un integer, aucun control
void fast_int2hexstr(unsigned int nombre, char *hexa_str)
{
	unsigned int div_par_16, reste, hexaindex;
	unsigned int i, swap;
	char hexadigit;
	hexaindex = 0;
	do
	{
		div_par_16 = nombre/16;
		reste = nombre-(div_par_16)*16;
		switch ( reste )
		{
			case 0 : hexadigit = '0'; break;
			case 1 : hexadigit = '1'; break;
			case 2 : hexadigit = '2'; break;
			case 3 : hexadigit = '3'; break;
			case 4 : hexadigit = '4'; break;
			case 5 : hexadigit = '5'; break;
			case 6 : hexadigit = '6'; break;
			case 7 : hexadigit = '7'; break;
			case 8 : hexadigit = '8'; break;
			case 9 : hexadigit = '9'; break;
			case 10 : hexadigit = 'A'; break;
			case 11 : hexadigit = 'B'; break;
			case 12 : hexadigit = 'C'; break;
			case 13 : hexadigit = 'D'; break;
			case 14 : hexadigit = 'E'; break;
			case 15 : hexadigit = 'F'; break;
		}
		hexa_str[hexaindex++] = hexadigit;
		nombre =  div_par_16;
	} while ( nombre > 0 );
  hexa_str[hexaindex] = '\0';
  //printf("HEXA = [%s]\n", hexa_str);
  // reverse
  for(i = 0; i < hexaindex/2; i++)
  {
	  swap = hexa_str[i];
	  hexa_str[i] = hexa_str[(hexaindex-1)-i];
	  hexa_str[(hexaindex-1)-i] = swap;
  }
}

unsigned long fast_hexstr2int(char *hexa_str)
{
	int i, len, rang = 1, multiplicateur, number = 0;
	// recherche la fin de la chaine
	len = 0;
	while( *(hexa_str+len) != '\0' )
	 len++;
	// commence par la fin : unites, dizaines, etc.
	for(i=len-1; i >= 0; i--)
	{
		if ( *(hexa_str+i) == 'x' ) break; // Fin de la chaine hexa
		switch( *(hexa_str+i) )
		{
			case '0' : multiplicateur = 0; break;
			case '1' : multiplicateur = 1; break;
			case '2' : multiplicateur = 2; break;
			case '3' : multiplicateur = 3; break;
			case '4' : multiplicateur = 4; break;
			case '5' : multiplicateur = 5; break;
			case '6' : multiplicateur = 6; break;
			case '7' : multiplicateur = 7; break;
			case '8' : multiplicateur = 8; break;
			case '9' : multiplicateur = 9; break;
			case 'a' :
			case 'A' : multiplicateur = 10; break;
			case 'b' :
			case 'B' : multiplicateur = 11; break;
			case 'c' :
			case 'C' : multiplicateur = 12; break;
			case 'd' :
			case 'D' : multiplicateur = 13; break;
			case 'e' :
			case 'E' : multiplicateur = 14; break;
			case 'f' :
			case 'F' : multiplicateur = 15; break;
			default : printf("NOT AN HEXADECIMAL NUMBER !\n"); return 0; break;
		}
		number = number + rang*multiplicateur;
		//printf("rang=%d, multi=%d -> number=%d\n", rang, multiplicateur, number);
		rang *= 16;
	}
  printf("DEC=%d\n", number);
  return number;
}





